// pages/record/record.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentYear: 0,
    currentMonth: 0,
    days: [], // 当月日期数据
    emptyDays: [], // 月初空白格子
    weekdays: ['日', '一', '二', '三', '四', '五', '六'], // 星期标识
    selectedDay: 0, // 选中的日期
    today: {
      // 今天的日期
      year: 0,
      month: 0,
      day: 0,
    },
    isRecord: true,
    timings: [
      {
        thing: '给火火称体重',
        dateTime: '8:00',
        complete: false,
        timingType: '1',
      },
      {
        thing: '铲屎',
        dateTime: '8:20',
        complete: false,
        timingType: '1',
      },
      {
        thing: '更换新的猫砂',
        dateTime: '12:20',
        complete: false,
        timingType: '4',
      },
    ],
    // 记录项数据
    recordItems: [
      {
        icon: '../../assets/record/饮食.png',
        content: '饮食',
        type: 'diet',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/喝水.png',
        content: '喝水',
        type: 'water',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/体重.png',
        content: '体重',
        type: 'weight',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/洗护.png',
        content: '洗护',
        type: 'grooming',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/尿便.png',
        content: '尿便',
        type: 'toilet',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/记事.png',
        content: '记事',
        type: 'memo',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/异常.png',
        content: '异常',
        type: 'abnormal',
        actionIcon: '../../assets/record/左.png'
      },
      {
        icon: '../../assets/record/用药.png',
        content: '用药',
        type: 'medicine',
        actionIcon: '../../assets/record/左.png'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 初始化日期为今天
    const today = new Date()
    this.setData({
      currentYear: today.getFullYear(),
      currentMonth: today.getMonth() + 1, // 月份从0开始，所以+1
      selectedDay: today.getDate(),
      today: {
        year: today.getFullYear(),
        month: today.getMonth() + 1,
        day: today.getDate(),
      },
    })

    // 生成当月日历
    this.generateCalendar()
  },
  
  // 生成日历数据
  generateCalendar() {
    const { currentYear, currentMonth } = this.data
    const daysInMonth = new Date(currentYear, currentMonth, 0).getDate() // 获取当月天数
    const firstDayOfMonth = new Date(currentYear, currentMonth - 1, 1).getDay() // 当月第一天是星期几(0-6)

    // 计算月初空白格子数量
    const emptyDays = []
    for (let i = 0; i < firstDayOfMonth; i++) {
      emptyDays.push(i)
    }

    // 生成当月日期数据
    const days = []
    for (let i = 1; i <= daysInMonth; i++) {
      // 判断是否是今天
      const isToday =
        currentYear === this.data.today.year &&
        currentMonth === this.data.today.month &&
        i === this.data.today.day

      // 判断是否是选中日期
      const isSelected = i === this.data.selectedDay

      days.push({
        day: i,
        isToday,
        isSelected,
      })
    }

    this.setData({
      days,
      emptyDays,
    })
  },
  
  // 选择日期
  selectDay(e) {
    const { day } = e.detail
    this.setData(
      {
        selectedDay: day,
      },
      () => {
        this.generateCalendar() // 重新生成日历以更新选中状态
      },
    )
  },

  // 上一个月
  prevMonth() {
    let { currentYear, currentMonth } = this.data

    if (currentMonth === 1) {
      currentMonth = 12
      currentYear--
    } else {
      currentMonth--
    }

    this.setData(
      {
        currentYear,
        currentMonth,
        selectedDay: 1, // 切换月份时默认选中1号
      },
      () => {
        this.generateCalendar()
      },
    )
  },

  // 下一个月
  nextMonth() {
    let { currentYear, currentMonth } = this.data

    if (currentMonth === 12) {
      currentMonth = 1
      currentYear++
    } else {
      currentMonth++
    }

    this.setData(
      {
        currentYear,
        currentMonth,
        selectedDay: 1, // 切换月份时默认选中1号
      },
      () => {
        this.generateCalendar()
      },
    )
  },
  
  // 切换tab
  switchTab(e) {
    const { type } = e.currentTarget.dataset
    this.setData({
      isRecord: type === 'record',
    })
  },
  
  addTiming() {
    wx.navigateTo({
      url: '/pages/addTiming/addTiming',
    })
  },
  
  /**
   * 切换提醒完成状态
   */
  toggleTimingComplete(e) {
    const index = e.currentTarget.dataset.index;
    const timings = this.data.timings;
    
    // 切换完成状态
    timings[index].complete = !timings[index].complete;
    
    this.setData({
      timings: timings
    });
  },
  
  // 记录项点击事件处理
  onRecordItemTap(e) {
    const { type, content } = e.detail;
    wx.navigateTo({
      url: `/pages/record-list/record-list?type=${type}&title=${content}`,
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
